package com.server.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.date.DateUtil;
import com.server.password.MyMD5;
import com.server.service.impl.CommonDataService;
import com.server.service.impl.MngUserService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller("LecturerController")
@RequestMapping("lecturer")
public class LecturerController {
	
	private String tableName = "education.admin_user";
	@Autowired
	private CommonDataService commonDataService;
	@Autowired
	private MngUserService mngUserService;
	@Login
	@RequestMapping("/{type}")
	public ModelAndView lecturer(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		String timeString  = request.getParameter("time");
		int id = 0;
		if(!StringUtils.isEmpty(timeString)){
			 String[] timeArray = timeString.split(",");
			 Long timeTemp = Long.parseLong(timeArray[0]);
			int week = DateUtil.dayInWeek(new Date(timeTemp));
			switch (week) {
			case 1:
				if(timeArray[1].equals("上午")){
					id = 1;
				}else if(timeArray[1].equals("下午")){
					id = 2;
				}
				break;
			case 2:
				if(timeArray[1].equals("上午")){
					id = 3;
				}else if(timeArray[1].equals("下午")){
					id = 4;
				}
				break;
			case 3:
				if(timeArray[1].equals("上午")){
					id = 5;
				}else if(timeArray[1].equals("下午")){
					id = 6;
				}
				break;
			case 4:
				if(timeArray[1].equals("上午")){
					id = 7;
				}else if(timeArray[1].equals("下午")){
					id = 8;
				}
				break;
			case 5:
				if(timeArray[1].equals("上午")){
					id = 9;
				}else if(timeArray[1].equals("下午")){
					id = 10;
				}
				break;
			case 6:
				if(timeArray[1].equals("上午")){
					id = 11;
				}else if(timeArray[1].equals("下午")){
					id = 12;
				}
				break;
			case 7:
				if(timeArray[1].equals("上午")){
					id = 13;
				}else if(timeArray[1].equals("下午")){
					id = 14;
				}
				break;

			default:
				id = 0;
				break;
			}
		}
		ModelAndView mv = new ModelAndView("lecturer/lecturerList");
		mv.addObject("type", type);
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("id", id);
		mv.addObject("timeString", timeString);
		return mv;
	}
	@Login
	@RequestMapping("list/{type}")
	public void list(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type) {
		String _whereStr = null;
		String nickName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		Integer level = SessionUtil.getIntAttri(request, SessionData.levelKey_mng);
		if(type==2){
			 _whereStr = "valid=1 and type=2";
		}else if(type==3){
			 _whereStr = "valid=1 and type=3";
		}
		if (1 == level) {
			
		}else {
			_whereStr += " and nickname='"+nickName+"'";
		}
		String sortStr = "createTime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(tableName, _whereStr, sortStr, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@Login
	@RequestMapping("check/{id}/{state}/{type}")
	public ModelAndView check(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/lecturer/lecturerForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("state", state);
		mv.addObject("type", type);
		mv.addObject("business", "lecturer");
		if(type==2){
			mv.addObject("titleName", "查看讲师信息");
		}else if(type==3){
			mv.addObject("titleName", "查看培训机构信息");
		}
		mv.addObject("userName", userName);
		return mv;
	}
	@Login
	@RequestMapping("add/{type}")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/lecturer/lecturerForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("type",type);
		mv.addObject("business", "lecturer");
		if(type==2){
			mv.addObject("titleName", "新增讲师信息");
		}else if(type==3){
			mv.addObject("titleName", "新增培训机构信息");
		}
		mv.addObject("userName", userName);
		return mv;
	}
	@Login
	@RequestMapping("save/{id}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String formData = request.getParameter("formData");
		Map<String, Object> saveMap = JSONSerielizer.toMap(formData);
		UUID uuid = UUID.randomUUID();
		Boolean isSuccess = true;
		String info = null;
		if (id.equals(0)) {
			String nickname  = saveMap.get("nickname").toString(); 
			isSuccess = mngUserService.check(nickname);//查询账号是否存在
			if(isSuccess){
				saveMap.put("uuid", uuid);
				String name = "";
				Object fieldObj = saveMap.get("field");
				if (fieldObj == null) {
					Object field1Obj = saveMap.get("field1");
					if (field1Obj == null) {
						
					}else {
						name = field1Obj.toString();
					}
					if(!StringUtils.isEmpty(name)){
						Integer ids = mngUserService.change(name, 4);
						saveMap.put("field", saveMap.get("field1"));
					}else{
						saveMap.put("field", saveMap.get("field2"));
					}
					saveMap.remove("field1");
					saveMap.remove("field2");
				}
				Object pswObj = saveMap.get("password");
				if(pswObj != null && !StringUtils.isEmpty(pswObj.toString())){
					saveMap.put("password", MyMD5.circularlyEncrypt(pswObj.toString(),3));
				}else{
					saveMap.put("password", MyMD5.circularlyEncrypt("123456",3));
				}
				isSuccess = commonDataService.add(operator, tableName, saveMap);
			}else{
				info = "账号已存在";
			}
			
		}else {
			String name = "";
			Object fieldObj = saveMap.get("field");
			if (fieldObj == null) {
				Object field1Obj = saveMap.get("field1");
				if (field1Obj == null) {
					
				}else {
					name = field1Obj.toString();
				}
				if(!StringUtils.isEmpty(name)){
					Integer ids = mngUserService.change(name, 4);
					saveMap.put("field", saveMap.get("field1"));
				}else{
					saveMap.put("field", saveMap.get("field2"));
				}
				saveMap.remove("field1");
				saveMap.remove("field2");
			}
			if(!StringUtils.isEmpty(name)){
				Integer ids = mngUserService.change(name, 4);
			}
			Object pswObj = saveMap.get("password");
			if(pswObj != null && !StringUtils.isEmpty(pswObj.toString())){
				saveMap.put("password", MyMD5.circularlyEncrypt(pswObj.toString(),3));
			}
			isSuccess = commonDataService.update(operator, tableName, "id", id.toString(), saveMap);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		map.put("info", info);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	@Login
	@RequestMapping("update/{id}/{state}/{type}")
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/lecturer/lecturerForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "edit");
		mv.addObject("type", type);
		mv.addObject("state", state);
		mv.addObject("business", "lecturer");
		if(type==2){
			mv.addObject("titleName", "编辑讲师信息");
		}else if(type==3){
			mv.addObject("titleName", "编辑培训机构信息");
		}
		mv.addObject("userName", userName);
		return mv;
	}
	@Login
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Integer estimate = mngUserService.deleteAdminUser(ids);
		OutputUtil.returnString(response, estimate.toString());
	}
}
